<template>
	<div class="admin">
		<el-card class="!border-none" shadow="never">
			<el-form class="mb-[-16px]" :model="formData" inline>
				<el-form-item label="名称">
					<el-input v-model="formData.title" class="w-40" size="small" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="resetPage" size="small">查询</el-button>
					<el-button @click="resetParams" size="small">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card v-loading="pager.loading" class="mt-2 !border-none" shadow="never">
			<el-button v-perms="['warehouse.warehouse/add']" type="primary" @click="handleAdd" size="small">
				<template #icon>
					<icon name="el-icon-Plus" />
				</template>
				新增
			</el-button>
			<div style="margin-top: 3px;">
				<el-table :data="pager.lists" size="small" border>
					<el-table-column label="序号" width="100" type="index" align="center"></el-table-column>
					<el-table-column label="名称" prop="title" min-width="170"></el-table-column>
					<el-table-column label="创建时间" prop="create_time" min-width="170"></el-table-column>
					<el-table-column label="备注" prop="remark" min-width="170"></el-table-column>
					<el-table-column label="操作" width="120" fixed="right" align="center">
						<template #default="{ row }">
							<el-button v-perms="['warehouse.warehouse/edit']" type="primary" link @click="handleEdit(row)">
								编辑
							</el-button>
							<el-button v-if="row.root != 1" v-perms="['warehouse.warehouse/delete']" type="danger" link
								@click="handleDelete(row.id)">
								删除
							</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="flex  justify-end">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
		<edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
	</div>
</template>

<script lang="ts" setup>
	import {
		adminjurisdictionLists,
		adminjurisdictiondelete
	} from '@/api/warehouse/list'
	import {
		usePaging
	} from '@/hooks/usePaging'
	import feedback from '@/utils/feedback'
	import EditPopup from './edit.vue'
	const editRef = shallowRef < InstanceType < typeof EditPopup >> ()
	// 表单数据
	const formData = reactive({
		// title: ''
	})
	const showEdit = ref(false)
	const {
		pager,
		getLists,
		resetParams,
		resetPage
	} = usePaging({
		fetchFun: adminjurisdictionLists,
		params: formData
	})

	const handleAdd = async () => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('add')
	}

	const handleEdit = async (data: any) => {
		
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.setFormData(data)
	}

	const handleDelete = async (id: number) => {
		await feedback.confirm('确定要删除？')
		await adminjurisdictiondelete({
			id
		})
		getLists()
	}
	onMounted(() => {
		getLists()
	})
</script>
